//
//  FMTabBar.swift
//  FleaMarket
//
//  Created by 李帅 on 16/10/24.
//  Copyright © 2016年 lishuai. All rights reserved.
//

import UIKit

class FMTabBar: UIView {

    var buttonWidth: CGFloat = 0
    var buttonArray: [UIButton] = []
    
    var selected_index: Int?
    
    var selectedIndex: Int {
        get { return self.selected_index! }
        
        set {
            self.selected_index = newValue
            
            for index in 0 ..< buttonArray.count {
                buttonArray[index].isSelected = (self.selected_index == index)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        let icon_normal_array = ["home_normal", "fishpond_normal", "", "message_normal", "account_normal"]
        let icon_highlight_array = ["home_highlight", "fishpond_highlight", "", "message_highlight", "account_highlight"]
        let title_array = ["闲鱼", "鱼塘", "", "消息", "我的"]
        
        buttonWidth = self.width / CGFloat(title_array.count)
        for index in 0 ..< title_array.count {
            
//            if index == 2 {
//                add_post_button()
//            }
            
            let button = TabBarButton(type: .custom)
            button.setTitle(title_array[index], for: .normal)
            button.setImage(UIImage(named: icon_normal_array[index]), for: .normal)
            button.setImage(UIImage(named: icon_highlight_array[index]), for: .selected)
//            button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
//            if index >= 2 {
//                button.frame = CGRect(x: CGFloat(index + 1) * buttonWidth, y: 0, width: buttonWidth, height: self.height)
//            } else {
                button.frame = CGRect(x: CGFloat(index) * buttonWidth, y: 0, width: buttonWidth, height: self.height)
//            }
            
            
            if index % 2 == 0 {
                button.backgroundColor = UIColor.blue
            }
            self.addSubview(button)
            buttonArray.append(button)
            
        }
        
    }
    
    func add_post_button() {
        
        let button = UIButton(type: .custom)
        button.setTitle("发布", for: .normal)
        button.titleLabel?.textAlignment = .center
        button.titleLabel?.font = UIFont.systemFont(ofSize: 8.0)
        button.adjustsImageWhenHighlighted = false
        button.setTitleColor(UIColor(hexString: "#333333"), for: .normal)
        button.setImage(UIImage(named: "post_normal"), for: .normal)
        button.addTarget(self, action: #selector(post_button_click), for: .touchUpInside)
        button.imageEdgeInsets = UIEdgeInsets(top: -30, left: 0, bottom: 0, right: 0)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
        button.frame = CGRect(x: 2 * buttonWidth, y: 0, width: buttonWidth, height: self.height)
        self.addSubview(button)
        button.backgroundColor = UIColor.orange
    }
    
    func post_button_click() {
        print("post click")
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
